<?php
/**
 * Created by PhpStorm.
 * User: syzhou
 * Date: 2019-08-28
 * Time: 12:35
 */

namespace App\Http\Controllers;


use App\Consts;
use App\Project;
use App\ReviewerScoreProject;
use App\Services\ReviewService;
use App\SysConfig;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class ReviewWebController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth', [
            'except' => [
                'index',
                'login',
            ],
        ]);
    }

    public function listProjects()
    {
        $user = Request::user();
        $row = DB::table('reviewer_group_reviewers as a')
            ->where('a.reviewer_id', $user['id'])
            ->leftJoin('large_groups as b', 'a.large_group_id', '=', 'b.id')
            ->select('a.large_group_id', 'b.award_id')
            ->first();
        $if_attend_review = $row !== null;
        if ($if_attend_review) {
            $award_id = $row->award_id;
            $large_group_id = $row->large_group_id;
        } else {
            $award_id = null;
            $large_group_id = null;
        }
        return view('review.list-projects', ['user' => $user, 'if_attend_review' => $if_attend_review,
            'award_id' => $award_id, 'large_group_id' => $large_group_id]);
    }

    public function awardManageMethod()
    {
        $user = Request::user();
        return view('review.award-manage-method', ['user' => $user]);
    }

    public function reviewWorkflow()
    {
        $user = Request::user();
        return view('review.review-workflow', ['user' => $user]);
    }

    public function feedback()
    {
        $user = Request::user();
        return view('review.feedback', ['user' => $user]);
    }

    public function contactUs()
    {
        $user = Request::user();
        return view('review.contact-us', ['user' => $user]);
    }

    public function score()
    {
        $user = Request::user();
        $project_id = Request::get('project_id');
        if ($project_id === null) {
            return $this->json(['ok' => false, 'msg' => "缺少参数: project_id"]);
        }
        (new ReviewService())->onScore($user->id, $project_id);
        $project = Project::find($project_id);
        $awardIssue = config('app.award_issue');
        $awardId = $project->award_id;
        $view = $awardId == Consts::AWARD_TYPE_PROGRESS ? 'review.progress-score' : 'review.talent-score';
        return view($view, ['project' => $project, 'awardIssue' => $awardIssue]);
    }
}
